/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.Container;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import who.tmap.ui.undo.UndoManager;
import who.tmap.ui.undo.impl.UndoManagerImpl;

import org.apache.log4j.Logger;

import who.tmap.toolkit.MappingToolKit;

/**
 * 
 * @author stl
 */
public class UserKeyListenerImpl extends KeyAdapter implements UserKeyListener {

    private static Logger logger = Logger.getLogger(UserKeyListenerImpl.class);  
    
    private static final UndoManager undoManager =UndoManagerImpl.getInstance();
    
    private MappingToolKit mtk;
    private UserMouseInputListener mil;
    private Container container;    
    
    public UserKeyListenerImpl() {
        super();
    }
    
    /**
     * @param container The container to set.
     */
    public void setContainer(Container container) {
        this.container = container;
    }
    
    /**
     * @param mtk The mtk to set.
     */
    public void setMtk(MappingToolKit mtk) {
        this.mtk = mtk;
    }        
    
	public void setMil(UserMouseInputListener mil) {
		this.mil = mil;
	}

	/**
	 * Make sure undo & redo are properly processed with delete & enter
	 */
	public void keyPressed(KeyEvent e) {    
	    		
		if ( e.getKeyChar() == KeyEvent.VK_DELETE ) {

			((JFrameMap)container).getJButtonDelete().doClick();
			
		} else if ( e.getKeyChar() == KeyEvent.VK_ENTER ) {

			mtk.closePolygon();
			container.repaint();
			
		} else if ( e.getKeyChar() == KeyEvent.VK_ESCAPE ) {
			
			mil.cancelCurrentEditing();
			container.repaint();
		
		} else if ( e.getKeyCode() == KeyEvent.VK_F5 ) {
			
			((JFrameMap)container).apply();
			
		} else if ( e.getKeyCode() == KeyEvent.VK_Z 
				&& ( ( e.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK ) == KeyEvent.CTRL_DOWN_MASK ) ) {
			
			try {
				undoManager.undo();
				container.repaint();
			} catch (Exception e1) {
				logger.error( e1 );
			}
			
		} else if ( e.getKeyCode() == KeyEvent.VK_Y 
				&& ( ( e.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK ) == KeyEvent.CTRL_DOWN_MASK ) ) {
			
			try {
				undoManager.redo();
				container.repaint();
			} catch (Exception e1) {
				logger.error( e1 );
			}
			
		}
	}    
    
}
